import app from '@/app'
import store from '@/app/store'
import { get, update, del } from '@/api/erp/pay'

// 初始化
const payStore = store()

payStore.get = () => {
  return new Promise((resolve, jeject) => {
    get().then(res => {
      payStore.setList(res)
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

payStore.update = (...arge) => {
  return new Promise((resolve, jeject) => {
    update(...arge).then(res => {
      payStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

payStore.del = (...arge) => {
  return new Promise((resolve, jeject) => {
    del(...arge).then(res => {
      payStore.get()
      resolve(res)
    }).catch(res => {
      jeject(res)
    })
  })
}

// 获取可使用的支付方式
payStore.all = (money = 0) => {
  // 限额检测
  const norm = (a, b) => {
    a = Number(a)
    b = Number(b)
    if(a > 0 && b > 0) {
      return a > b
    }
    return true
  }
  return payStore.getList().filter(item => item.status && norm(item.norm, money))
}

// 获取默认支付方式
payStore.default = () => {
  return payStore.getList()[0] ?? {}
}

payStore.id = id => {
  return payStore.getList().find(item => item.id == id)
}

// 获取支付方式名称
payStore.name = id => {
  const pay = payStore.id(id)
  return pay ? pay.name : ''
}

export default payStore